require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
// if(!userInfo){
//     location.href="/loginIndex"
// }
var vm = new Vue({
    el:"#travelsInfo",
    data:{
        commentTab:1,
        loadFlag:false,
        isInfoShow:false,
        isGoods:false,
        isComment:false,//评论显示隐藏
        indexs:1,
        uid:"",//用户id
        praiseState:"",//点赞状态
        praiseCount:"",//点赞人数
        collectionState:"",//收藏状态
        collectionCount:"",//收藏总数
        // rewardCount:"",
        pageNumber:"1",
        cityAdvId:"",
        cityAdvInfo:{},
        cityAdvList1:[],
        cityAdvList2:[],//人气列表
        commentList:{},
        commentList1:[],
        commentList2:{},
        rewardCount:"",//已打赏金额
        isReward:"",
        isRewardNum:1,
        moneyList:[{value:"50"},{value:"100"},{value:"200"},{value:"200"},{value:"1000"},{value:"2000"}],
        rewardMoney:"",
        money:"", //打赏金额
        commentText:"",
        topBg:false,
        isShare:false
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.cityAdvId=this.searchObject&&this.searchObject.cityAdvId;
        // if(this.searchObject&&this.searchObject.loginUid){
        //     this.uid=this.searchObject&&this.searchObject.loginUid;
        // }else{
        //     if(!userInfo){
        //         location.href="/loginIndex"
        //     }else{
        //         this.uid=userInfo.id;
        //     }
        // }
        this.getCityAdvInfo();
    },
    methods:{
        //后退
        getBack:function(){
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //获取城市游记详情
        getCityAdvInfo:function(){
            var that=this;
            var data={
                cityAdvId:that.cityAdvId
                // loginUid:this.uid
            };
            this.ajax({url:"/cityAd/getCityAdvInfo",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.cityAdvInfo=b.json.data;
                    that.praiseState=that.cityAdvInfo.praiseState;
                    that.praiseCount=that.cityAdvInfo.praiseCount;
                    that.collectionState=that.cityAdvInfo.collectionState;
                    that.collectionCount=that.cityAdvInfo.collectionCount;
                    that.rewardCount=that.cityAdvInfo.rewardCount;
                    console.log(that.cityAdvInfo)
                    console.log(that.collectionState)
                    console.log(that.praiseState)
                    // that.cityAdvList1=that.cityAdvList.list;
                    // if(that.cityAdvList.pageNumber>1){
                    //     if(that.cityAdvList.list.length>0) {
                    //         that.cityAdvList2 = that.cityAdvList2.concat(b.json.data.list);
                    //     }
                    // }else{
                    //     that.cityAdvList2= that.cityAdvList1;
                    // }
                    // vm.loadFlag=false;
                    // lazyload();
                }else{
                    vm.$refs.prompt.toast("获取游记失败！")
                }
            });
        },
        //通过用户id 视频id state点赞状态 1点赞0取消点赞
        getPraise:function(){
            if(!userInfo){
                location.href="/loginIndex?number=17&productId="+this.cityAdvId;
                return false;
            }
            var that=this;
            var data={
                loginUid:that.uid||userInfo.id,
                cityAdvId:that.cityAdvId
            };
            if(that.praiseState==0){
                data.state=1;
            }else{
                data.state=0;
            }
            console.log(data);
            this.ajax({url:"/cityAd/getCityAdPraise",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.getCityAdvInfo();
                    // console.log(that.praiseCount)
                }else{
                    vm.$refs.prompt.toast("点赞失败！")
                }
            });
        },
        //通过用户id 视频id state收藏状态 1收藏0取消收藏
        getCollection:function(){
            if(!userInfo){
                location.href="/loginIndex?number=17&productId="+this.cityAdvId;
                return false;
            }
            var that=this;
            var data={
                loginUid:that.uid||userInfo.id,
                cityAdvId:that.cityAdvId
            };
            if(that.collectionState==0){
                data.state=1;
            }else{
                data.state=0;
            }
            console.log(data)
            this.ajax({url:"/cityAd/getCityAdCollection",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.getCityAdvInfo();
                    //console.log(that.collectionState)
                }else{
                    vm.$refs.prompt.toast("收藏失败！");
                }
            });
        },
        //显示评论
        getComment:function(type){
            if(!userInfo){
                location.href="/loginIndex?number=17&productId="+this.cityAdvId;
                return false;
            }
            // this.commentTab=type;
            // if(type==2){
            //
            // }
            //console.log(this.cityAdvInfo.commentCount)
            this.getInfoMediaAdvCommentList();
            //this.getInfoMediaAdvCommentList();
            this.isComment=!this.isComment;
        },
        // //tab切换
        // getCommentTab:function(num){
        //     this.commentTab=num;
        //     if(num==2){
        //         this.getInfoMediaAdvCommentList();
        //     }
        // },
        getGoods:function(){
            this.isGoods=!this.isGoods;
        },
        //时间
        getData:function(data){
            return data.split(' ')[0];
        },
        //获取自媒体评论列表
        getInfoMediaAdvCommentList:function(){
            var that=this;
            var data={
                loginUid:that.uid||userInfo.id,
                id:that.cityAdvId,
                pageNumber:that.pageNumber
            };
            //console.log(data)
            this.ajax({url:"/selfMedia/getInfoMediaAdvCommentList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==00&&b.json.data){
                    that.commentList=b.json.data;
                    that.commentList1=that.commentList.list;
                    if(that.commentList.pageNumber>1){
                        if(that.commentList.list.length>0) {
                            that.commentList2 = that.commentList2.concat(b.json.data.list);
                        }
                    }else{
                        that.commentList2= that.commentList1;
                    }
                    vm.loadFlag=false;
                    console.log(that.commentList)
                }else{
                    that.commentList={};
                    that.commentList1=[];
                    that.commentList2=[];
                    vm.$refs.prompt.toast("获取评论列表！");
                }
            });
        },
        getReward:function(num){
            // this.isRewardNum=1;
            this.isReward=num;
        },
        getIsRewardNum:function(num){
            // this.rewardMoney="";
            this.isRewardNum=num;
        },
        //自媒体打赏
        mediaReward:function(){
            if(!userInfo){
                location.href="/loginIndex?number=17&productId="+this.cityAdvId;
                return false;
            }
            var that=this,data;
            // if(money){that.money=money;}
            // console.log(that.money)
            // if(!that.money){
            //     vm.$refs.prompt.toast("打赏金额为空，不能打赏！");
            //     return false;
            // }else if(that.money<1){
            //     that.rewardMoney="";
            //     vm.$refs.prompt.toast("打赏金额最小为1乐币！");
            //     return false;
            // }
            data={
                cityAdvId:that.cityAdvId,
                loginUid:that.uid||userInfo.id,
                // money:that.money
                money:1
            };
            console.log(data)
            this.ajax({url:"/cityAd/cityAdReward",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    // that.mediaRewardInfo=b.json.data;
                    that.rewardCount=b.json.data;
                    that.getReward(1);
                    c=setInterval(function(){
                        that.getReward();
                    },3000);
                    // vm.$refs.prompt.toast("打赏成功！");
                    // that.getReward();
                    that.getCityAdvInfo();
                    console.log(b.json.data)
                }else{
                    if(b.json.msg=="金币不够"){
                        that.getReward(2);
                        c=setInterval(function(){
                            that.getReward();
                        },3000);
                    }else{
                        vm.$refs.prompt.toast("打赏失败！");
                    }
                }
            });
        },
        //评论点赞
        praiseMediaAdvComment:function(state,commentId){
            if(!userInfo){
                location.href="/loginIndex?number=17&productId="+this.cityAdvId;
                return false;
            }
            var that=this;
            console.log(state+"----"+commentId)
            var data={
                loginUid:that.uid||userInfo.id,
                commentId :commentId
            };
            if(state==0){
                data.state=1;
            }else{
                data.state=0;
            }
            console.log(data)
            this.ajax({url:"/selfMedia/praiseMediaAdvComment",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.getInfoMediaAdvCommentList();
                    // that.commentList2.forEach(function(val){
                    //     if(val.commentId==commentId){
                    //         val.praiseState=data.state;
                    //         val.praiseCount=b.json.data.praiseCount;
                    //     }
                    // });
                    console.log(b.json.data)
                }else{
                    vm.$refs.prompt.toast("点赞失败！");
                }
            });
        },
        //发表评论
        comment:function(){
            var that=this;
            if(!that.commentText){
                vm.$refs.prompt.toast("请输入评论内容！");
                return false;
            }
            var data={
                loginUid:that.uid||userInfo.id,
                content:that.commentText,
                id:that.cityAdvId
            };
            console.log(data);
            this.ajax({url:"/selfMedia/comment",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.commentText="";
                    that.getCityAdvInfo();
                    that.getInfoMediaAdvCommentList();
                    //console.log(b.json.data)
                }else{
                    vm.$refs.prompt.toast("评论失败！");
                }
            });
        },
        getCommentInfo:function(commentId){
            location.href="/see/commentInfo?id="+this.cityAdvId+"&loginUid="+this.uid+"&commentId="+commentId;
        },
        //跳转页面
        getNext:function(recruitId){
            location.href="/see/jobDetails?recruitId="+recruitId;
        },
        getShare:function(){
            this.isShare=!this.isShare;
        },
        getChoice:function(num){
            if(num==1){
                location.href="/friend/choiceList?number=5&postId="+this.cityAdvId;
            }else{
                location.href="/friend/forward?number=5&postId="+this.cityAdvId;
            }

        }
    }
    ,watch:{
        isGoods:function(){
            if(this.isGoods&&this.cityAdvInfo.products&&this.cityAdvInfo.products.length>0){
                this.$nextTick(function(){
                    var swiper1 = new Swiper(".selfMediaVd-goodsCent .swiper-container",{
                        spaceBetween: 5,
                        slidesPerView: 'auto',
                        freeMode: true,
                        lazy: {
                            loadPrevNext: true,
                            loadPrevNextAmount: 5,
                            loadOnTransitionStart: true
                        },
                        observer: true,//修改swiper自己或子元素时，自动初始化swiper
                        observeParents: true//修改swiper的父元素时，自动初始化swiper
                    });
                });
            }else{
                vm.$refs.prompt.toast("没有商品售卖！");
            }
        },
        commentList2:function(){
            this.$nextTick(function(){
                console.log($(".comment-cent").height());
                getScroll();
            });
        },
        rewardMoney:function(){
            this.money=Number(this.rewardMoney);
        }

    }
});
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}
window.onscroll=function(){
    if(getDocumentTop()>0){
        vm.topBg=true;
    }else{
        vm.topBg=false;
    }
};
function getScroll(){
    var nScrollHight = 0; //滚动距离总长(注意不是滚动条的长度)
    var nScrollTop = 0;   //滚动到的当前位置
    var nDivHight = $(".comment-cent").height();
    console.log(nDivHight)
    $(".comment-cent").scroll(function(){
        var loadFlag = vm.commentList.lastPage;
        //监听事件内容
        nScrollHight = $(this)[0].scrollHeight;
        nScrollTop = $(this)[0].scrollTop;
        if(nScrollTop + nDivHight >= nScrollHight){
            if(!loadFlag){
                vm.loadFlag=true;
                vm.pageNumber++;
                vm.getInfoMediaAdvCommentList();
            }else{
                vm.$refs.prompt.toast("没有更多了！")
            }
        }
    });
}